#################################
#-*- coding: utf-8 -*-
#     File name   :read.py
#     Author      :kangkangliang
#     File desc   :read xls
#     Mail        :liangkangkang@yahoo.com
#     Create time :2016-11-29
#################################
#!/usr/bin/env python
from pymongo import MongoClient
from  xlrd import open_workbook,cellname
import sys

client = MongoClient()

## get dataname
# db = client.test
db = client.info

## get set
collection = db.gov

## get filename
fname = sys.argv[1]
book = open_workbook(fname)
sheet = book.sheet_by_index(0)

print "table name:",sheet.name

print "rows:",sheet.nrows
print "cols:",sheet.ncols

for row_index in range(sheet.nrows):
# for row_index in range(21):
    if row_index == 0:
        continue
    lt = []
    for col_index in range(sheet.ncols):
        # print cellname(row_index,col_index),'-',
        lt.append(sheet.cell(row_index,col_index).value)
        # print sheet.cell(row_index,col_index).value,
    # print "\n"
    # print lt
    # print len(lt)
    # define var post
    post = {"domain":lt[0],"ip":lt[1],"Registrar":lt[2],"Registration_Time":lt[3],"deadline":lt[4],"Sponsoring Registrar":lt[5],"Name Server":lt[6],"Domain status:":lt[7],"Registrar_Email":lt[8],"collection_time":lt[9]}
    ## insert Documents
    collection.insert(post)
    # print row_index

print "ok..."
"""
for row_index in range(sheet.nrows):
    for col_index in range(sheet.ncols):
        print cellname(row_index,col_index),'-',
        print sheet.cell(row_index,col_index).value
"""

# if __name__ = "__main__":
    # main()

#xlsx time transform format
# import datetime,xlrd
# deadline = 45074.7298611
# ti = 42992.4354167

# book = xlrd.open_workbook("./Gov.xlsx")
# # sh = book.sheet_by_index(0)
# # a1 = sh.cell_value(rowx=0, colx=0)
# a1_as_datetime = datetime.datetime(*xlrd.xldate_as_tuple(ti, book.datemode))
# print 'datetime: %s' % a1_as_datetime

# year, month, day, hour, minute, second = xlrd.xldate_as_tuple(ti, book.datemode)
# whatYouWant = str(year)+'/'+str(month)+'/'+str(day)+'/'+str(hour)+':'+str(minute)+':'+str(second)
# print whatYouWant

